#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "GenList.h"

void InitGenList(GenList * gl)
{

}

int CreateGenList(GenList *gl, char *str)
{
     int n = strlen(str);
     char *sub = malloc(sizeof(char) * (n - 2));
     char *hsub = malloc(sizeof(sizeof(char) * (n - 2)));
     if (sub == NULL || hsub == NULL)
          return -1;
     strncpy(sub, str + 1, n - 2);
     sub[n - 2] = '\0';
     if(*gl == NULL)
     {
          *gl = malloc(sizeof(GLNode));
          if(*gl == NULL)
               return -2;
          (*gl)->tag = HEAD;
          (*gl)->hp = (*gl)->tp = NULL;
     }
     GLNode *p = *gl;
     while(strlen(sub) != 0)
     {
          p = p->tp = malloc(sizeof(sizeof(GLNode)));
          if(p == NULL)
               return -3;
          p->hp = p->tp = NULL;
          //"1,2,3"   分离表头 ==> hsub = "1" sub = "2,3"
          //"(1,2),3"   分离表头 ==> "(1,2)" hsub = "(1,2)" sub = "3,4"
          if(sever(sub,hsub))
          {
               if(hsub[0] == '(')
               {
                    p->tag = CHILDLIST;
                    CreateGenList(&p->hp,hsub);
               }
               else
               {
                    p->tag = ATOM;
                    p->atom = atoi(hsub);
               }
          }
     }

}